package com.yunnuo.com.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunnuo.com.core.PageReq;
import com.yunnuo.com.core.ResponseResult;
import com.yunnuo.com.domain.dto.SysUserReqDto;
import com.yunnuo.com.domain.entity.SysUser;
import com.yunnuo.com.mapper.product.ProductBusinessDao;
import com.yunnuo.com.pojo.product.ProductBusiness;
import com.yunnuo.com.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author yunnuo
 * @since 2022-04-09
 */
@Valid
@Slf4j
@RequestMapping("/api/user")
@RestController
public class SysUserController {

    private final SysUserService service;

    public SysUserController(SysUserService service) {
        this.service = service;
    }

    @PostMapping("/page")
    public ResponseResult<IPage<SysUser>> pageUsers(@RequestBody @Validated PageReq pageReq) {
        return ResponseResult.success(service.pageUsers(pageReq));
    }

    @GetMapping("/getByUserId/{id}")
    public ResponseResult<String> getLibraryAndTableByUserId(@PathVariable Long id) {
        return ResponseResult.success(service.getLibraryAndTableByUserId(id));
    }


    @PostMapping("/add")
    public ResponseResult<SysUser> add(@RequestBody @Validated SysUserReqDto reqDto) {
        return ResponseResult.success(service.saveUserInfo(reqDto));
    }

    @Resource
    private ProductBusinessDao productBusinessDao;
    @GetMapping("client")
    public ResponseResult client(Long client) {
        ProductBusiness productBusiness = new ProductBusiness();
        productBusiness.setClient(client);
        productBusiness.setName(String.valueOf(client));
        productBusinessDao.insertSelective(productBusiness);
        return ResponseResult.success();
    }

}
